<?php


namespace app\model;

use app\common\library\Translation;
use think\facade\Db;
use think\Model;
use app\model\Attachment as AttachmentModel;
use app\model\ConfigModel;

class GoodsModel extends Model
{
    protected $table = "store_goods";

    /**
     * @param $data
     * @return GoodsModel|array|mixed|string|Model|null
     * @notes 货物添加
     * @author: dongdong
     * @date: 2025/1/1 11:24
     */
    public function add($data){
        Db::startTrans();
        try{
            $Translation = new Translation();

            $title_arr = $Translation->translationSelf($data['title']??'');
            $param['title'] = $title_arr['zh'];
            $param['title_ru'] = $title_arr['ru'];
            $param['user_id'] = $data['user_id'];
            $param['param'] = json_encode($data);
            $param['create_at'] = date('Y-m-d H:i:s');

            $param['package_id'] = $data['package_id']??0;
            $config = new ConfigModel();

            if($param['package_id']==0){
                if(!empty($data['package_name'])){
                    $package_name = $Translation->translationSelf($data['package_name']??'');
                    $d['title'] = $package_name['zh'];
                    $d['title_ru'] = $package_name['ru'];
                    $d['type'] = 1;
                    $d['user_id'] = $data['user_id'];
                    $d['create_at'] = date('Y-m-d H:i:s');
                    $param['package_id'] = $config->add($d);
                }
            }


            $param['unload_id'] = $data['unload_id']??0;
            $param['goods_weight'] = $data['goods_weight'];
            $param['goods_volume'] = $data['goods_volume'];

            $param['goods_l'] = $data['goods_l']??0;
            $param['goods_w'] = $data['goods_w']??0;
            $param['goods_h'] = $data['goods_h']??0;

            $param['goods_num'] = $data['goods_num']??0;

            $insertGetId = self::insertGetId($param);


            $Attachment = new AttachmentModel();
            $attachment_id = '';
            if(!empty($data['image'])){
                $arr = explode(',',$data['image']);
                foreach ($arr as $k =>&$v){
                    $attachment_id .= $Attachment->insertGetId(['user_id'=>$param['user_id'],'table_name'=>$this->table,'table_id'=>$insertGetId,'path'=>$v]).',';
                }
                $attachment_id = trim($attachment_id,',');

                self::where(["id"=>$insertGetId])->update(['attachment_id'=>$attachment_id]);
            }

            $data = self::find($insertGetId);

            $data['package'] = $config->info($data['package_id']);
            $data['unload'] = $config->info($data['unload_id']);
            $data['attachment'] = $Attachment->field("id,path")->where("id","in",$data['attachment_id'])->select();

            Db::commit();
            return $data;
        }catch (\Exception $e){
            Db::rollback();
            return '';
        }
    }

    /**
     * @param $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 返回商品详情
     * @author: dongdong
     * @date: 2025/1/8 14:23
     */
    public function info($goods_id){
        $data = self::where(['id'=>$goods_id])->find();
        if($data){
            $config = new ConfigModel();
            $Attachment = new AttachmentModel();

            $goods['title'] = $data['title'];
            $goods['title_ru'] = $data['title_ru'];
            $goods['goods_weight'] = $data['goods_weight'];
            $goods['goods_volume'] = $data['goods_volume'];
            $goods['goods_l'] = $data['goods_l'];
            $goods['goods_w'] = $data['goods_w'];
            $goods['goods_h'] = $data['goods_h'];
            $goods['goods_num'] = $data['goods_num'];
            $goods['package'] = $config->info($data['package_id']);
            $goods['unload'] = $config->info($data['unload_id']);
            $goods['attachment'] = $Attachment->field("id,path")->where("id","in",$data['attachment_id'])->select();
            return $goods;
        }else{
            return [];
        }

    }




}